/* eslint-disable no-param-reassign */
/* eslint-disable no-restricted-syntax */
/* eslint-disable guard-for-in */
/* eslint-disable no-plusplus */
import fs from 'file-saver'
import Excel from 'exceljs'
import dayjs from 'dayjs'

// const s2ab = (s: string) => {
//   if (typeof ArrayBuffer !== 'undefined') {
//     const buf = new ArrayBuffer(s.length)
//     const view = new Uint8Array(buf)
//     for (let i = 0; i !== s.length; ++i) view[i] = s.charCodeAt(i) & 0xff
//     return buf
//   }
//   const buf = new Array(s.length)
//   for (let i = 0; i !== s.length; ++i) buf[i] = s.charCodeAt(i) & 0xff
//   console.log(buf)
//   return buf
// }

const columsExcel = (title: string, columns: Array<any>, Data: Array<any>) => {
  const dataList: Array<any> = []
  const columnsList: Array<any> = []
  dataList.push([title])
  for (let i = 0; i < columns.length; i++) {
    columnsList.push(columns[i].title)
  }
  dataList.push(columnsList)
  for (let i = 0; i < Data.length; i++) {
    const rowData: Array<any> = []
    for (let j = 0; j < columns.length; j++) {
      if (columns[j].types === 'text' || columns[j].types === undefined) {
        rowData.push(Data[i][columns[j].key])
      }
    }
    dataList.push(rowData)
  }
  return dataList
}

export default (
  konvaData: string,
  babylonData: string,
  fields: {
    fence: { columns: Array<any>; data: Array<any> }
    door: { columns: Array<any>; data: Array<any> }
    pillar: { columns: Array<any>; data: Array<any> }
    gadget: { columns: Array<any>; data: Array<any> }
  },
  fileName: string = `vichdraw ${dayjs().format('YYYY-MM-DD HH-MM')}`
) => {
  const workbook = new Excel.Workbook()
  workbook.created = new Date()
  workbook.modified = new Date()

  const konvaImage = workbook.addImage({
    base64: konvaData,
    extension: 'jpeg'
  })
  const babylonImage = workbook.addImage({
    base64: babylonData,
    extension: 'jpeg'
  })
  const worksheet = workbook.addWorksheet('vichdraw')
  worksheet.mergeCells('A1', 'H1')
  const titleCell = worksheet.getCell('A1')
  titleCell.alignment = { vertical: 'middle', horizontal: 'center' }
  titleCell.font = {
    name: 'Comic Sans MS',
    family: 4,
    size: 16,
    bold: true
  }
  titleCell.value = fileName
  worksheet.addImage(konvaImage, 'J2:R16')
  worksheet.addImage(babylonImage, 'J18:R32')
  const fenceData = columsExcel('', fields.fence.columns, fields.fence.data)
  const doorData = columsExcel('', fields.door.columns, fields.door.data)
  const pillarData = columsExcel('', fields.pillar.columns, fields.pillar.data)
  const gadgetData = columsExcel('', fields.gadget.columns, fields.gadget.data)
  const data = fenceData.concat(doorData).concat(pillarData).concat(gadgetData)
  for (let i = 0; i < data.length; i++) {
    worksheet.getRow(i + 3).values = data[i]
  }
  workbook.xlsx.writeBuffer().then((buffer: ArrayBuffer) => {
    ;(fs as any)(
      new Blob([buffer], {
        type: 'application/octet-stream'
      }),
      `${fileName}.xlsx`
    )
  })
}
